package com.mach.platform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mach.platform.domain.ProductionOrder;
import com.mach.platform.domain.ProductionOrderTerm;
import com.mach.platform.domain.Response;
import com.mach.platform.service.ProductionOrderServiceImpl;
import com.mach.platform.service.ProductionOrderTermServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.*;

/**
 * 生产项控制器
 */

@RestController
@RequestMapping("productionOrderTerm")
public class ProductionOrderTermCtrl extends BaseCtrl<ProductionOrderTerm> {

    @Autowired
    private ProductionOrderTermServiceImpl productionOrderTermService;

    @Autowired
    private ProductionOrderServiceImpl productionOrderService;

    @InitBinder("productionOrderTerm")
    public void initBinderProductionOrderTerm(WebDataBinder binder){
        binder.setFieldDefaultPrefix("productionOrderTerm.");
    }

    @InitBinder("productionOrder")
    public void initBinderProductionOrder(WebDataBinder binder){
        binder.setFieldDefaultPrefix("productionOrder.");
    }


    @PostMapping("/add")
    public Response add(@ModelAttribute("productionOrderTerm") ProductionOrderTerm productionOrderTerm,
                        @ModelAttribute("productionOrder") ProductionOrder productionOrder) {
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("student", productionOrderTerm);
        map.put("course", productionOrder);
        return Response.ok(map);
    }

    @ApiOperation(value = "保存", notes = "接口备注：无", produces = "application/json")
    @Transactional
    @PostMapping("/addParams")
    public Response add(@RequestBody Map params) {
        int result = 1;
        List<LinkedHashMap> productionOrderTermMap = new ArrayList<>(32);
        ProductionOrder productionOrder = new ProductionOrder();
        if (params != null && params.size() > 0){
            productionOrderTermMap = (List<LinkedHashMap>) params.get("productionOrderTerm");
            productionOrder = JSON.parseObject(JSONObject.toJSONString(params.get("productionOrder"),true),ProductionOrder.class);
        }
        productionOrder = productionOrderService.save(productionOrder);
        productionOrderTermService.add(productionOrderTermMap, productionOrder);
        return Response.ok(result);
    }

    @ApiOperation(value = "修改", notes = "接口备注：无", produces = "application/json")
    @Transactional
    @PostMapping("/updateParams")
    public Response update(@RequestBody Map params) {
        int result = 1;
        List<LinkedHashMap> productionOrderTermMap = new ArrayList<>(32);
        ProductionOrder productionOrder = new ProductionOrder();
        if (params != null && params.size() > 0){
            productionOrderTermMap = (List<LinkedHashMap>) params.get("productionOrderTerm");
            productionOrder = JSON.parseObject(JSONObject.toJSONString(params.get("productionOrder"),true),ProductionOrder.class);
        }
        productionOrder = productionOrderService.update(productionOrder);
        productionOrderTermService.update(productionOrderTermMap, productionOrder);
        return Response.ok(result);
    }

    @Override
    public Response info(Long id) {
        return null;
    }

    /**
     * 分页查询
     * @param pageable
     * @param params
     * @return
     */
    @GetMapping("/findByPage")
    @Override
    public Response list(@PageableDefault(size = 50)Pageable pageable, @RequestParam Map params) {
//        List<ProductionOrder> productionOrders = productionOrderService.getProductionOrder(pageable, params);
        Page<ProductionOrder> productionOrders = productionOrderService.list(pageable, params);

        List<Long> productionOrderIds = Lists.newArrayList();
        if (productionOrders != null && productionOrders.getSize() > 0){
            productionOrders.forEach(item -> productionOrderIds.add(item.getId()));
        }
        List<ProductionOrderTerm> productionOrderTerms = productionOrderTermService.getListByProductionOrdersIds(productionOrderIds);
        Map result = Maps.newHashMap();
        result.put("productionOrder", null);
        if (productionOrders != null && productionOrders.getContent().size() > 0){
            result.put("productionOrder", productionOrders.getContent().get(0));
        }
        result.put("productionOrderTerms", productionOrderTerms);
        return Response.ok(result);
    }

    @ApiOperation(value = "删除", notes = "接口备注：无", produces = "application/json")
    @DeleteMapping("/delete")
    @Override
    public Response delete(@RequestBody Long[] ids) {
        int result = productionOrderTermService.delete(ids);
        return Response.ok(result);
    }

    @Override
    public Response save(ProductionOrderTerm productionOrderTerm) {
        return null;
    }

    @Override
    public Response update(ProductionOrderTerm productionOrderTerm) {
        return null;
    }

}
